import 'package:flutter/material.dart';

class FloatIcons extends StatefulWidget {
  FloatIcons({this.icons, this.onIconTapped});
  final List<IconData> icons;
  final ValueChanged<int> onIconTapped;
  @override
  createState() => FloatIconsState();
}

class FloatIconsState extends State<FloatIcons> with TickerProviderStateMixin {
  AnimationController _controller;
  @override
  initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 250),
    );
  }

  @override
  build(context) => Column(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children:
            List.generate(widget.icons.length, (index) => _buildChild(index))
                .toList()
                  ..add(
                    FloatingActionButton(
                        onPressed: () => _controller.isDismissed
                            ? _controller.forward()
                            : _controller.reverse(),
                        tooltip: '点击弹出',
                        elevation: 0.0,
                        child: Icon(Icons.add)),
                  ),
      );
  Widget _buildChild(index) => Container(
        height: 70.0,
        width: 56.0,
        alignment: FractionalOffset.topCenter,
        child: ScaleTransition(
          scale: CurvedAnimation(
            parent: _controller,
            curve: Interval(0.0, 1.0 - index / widget.icons.length / 2.0,
                curve: Curves.easeOut),
          ),
          child: FloatingActionButton(
            backgroundColor: Theme.of(context).cardColor,
            mini: true,
            child:
                Icon(widget.icons[index], color: Theme.of(context).accentColor),
            onPressed: () => widget.onIconTapped(index),
          ),
        ),
      );
}
